<?php

/**
 * ECSHOP 会员等级管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: banner.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($ecs->table("banner"), $db, 'id', 'reg_field_name');
$allow_suffix = array('gif', 'jpg', 'png', 'jpeg', 'bmp');
/*------------------------------------------------------ */
//-- 会员注册项列表
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    $fields = array();
    $fields = $db->getAll("SELECT * FROM " . $ecs->table('banner') . " ORDER BY u_time desc, id desc");

    $smarty->assign('ur_here',      'Banner设置');
    $smarty->assign('action_link',  array('text' => '添加banner', 'href'=>'banner.php?act=add'));
    $smarty->assign('full_page',    1);

    $smarty->assign('banner',   $fields);

    assign_query_info();
    $smarty->display('banner.htm');
}


/*------------------------------------------------------ */
//-- 翻页，排序
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $fields = array();
    $fields = $db->getAll("SELECT * FROM " .$ecs->table('banner') . "ORDER BY u_time desc,id desc");

    $smarty->assign('banner',   $fields);
    make_json_result($smarty->fetch('banner.htm'));
}

/*------------------------------------------------------ */
//-- 添加会员注册项
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add')
{
    admin_priv('banner');

    $form_action = 'insert';
    $smarty->assign('ur_here',     '添加banner');
    $smarty->assign('action_link', array('text' => 'Banner设置', 'href'=>'banner.php?act=list'));
    $smarty->assign('form_action', $form_action);

    $smarty->assign('width_height', sprintf('此模板的图片标准宽度为：%s 标准高度为：%s', 1920, 600));

    assign_query_info();
    $smarty->display('banner_add.htm');
}

/*------------------------------------------------------ */
//-- 增加会员注册项到数据库
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert')
{
    // var_dump(444);
    // var_dump($_POST);
    // var_dump($_FILES);exit;
    // admin_priv('banner');

    // 生成图片
    if (!empty($_FILES['img']['name']))
    {
        if(!get_file_suffix($_FILES['img']['name'], $allow_suffix))
        {
            sys_msg('您上传的图片格式不正确！');
        }
        $name = date('Ymd');
        for ($i = 0; $i < 6; $i++)
        {
            $name .= chr(mt_rand(97, 122));
        }
        $bbb = explode('.', $_FILES['img']['name']);
        $name .= '.' . end($bbb);
        $target = ROOT_PATH . DATA_DIR . '/banner/' . $name;
        if (move_upload_file($_FILES['img']['tmp_name'], $target))
        {
            $img = DATA_DIR . '/banner/' . $name;
        }
    }else
        {
            $links[] = array('text' => '添加banner', 'href' => 'banner.php?act=add');
            sys_msg('请填写上传banner图片', 0, $links);
        }
    // var_dump($img);exit;
    $time = time();
    $sql = "INSERT INTO " .$ecs->table('banner') ."( ".
                "img, c_time, u_time".
            ") VALUES (".
                "'$img', '$time', '$time')";
    $db->query($sql);

    /* 管理员日志 */
    // admin_log($img, 'add', 'banner');
    clear_cache_files();

    $lnk[] = array('text' => 'Banner设置',    'href'=>'banner.php?act=list');
    $lnk[] = array('text' => '添加banner', 'href' => 'banner.php?act=add');
    sys_msg('添加Banner成功', 0, $lnk);
}

/*------------------------------------------------------ */
//-- 编辑会员注册项
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit')
{
    admin_priv('banner');
    $smarty->assign('width_height', sprintf('此模板的图片标准宽度为：%s 标准高度为：%s', 1920, 600));
    $form_action = 'update';

    $sql = "SELECT id,img FROM ".
           $ecs->table('banner'). " WHERE id='$_REQUEST[id]'";
    $one = $db->GetRow($sql);

    $smarty->assign('one',  $one);
    $smarty->assign('ur_here',     '编辑banner');
    $smarty->assign('action_link', array('text' => 'Banner设置', 'href'=>'banner.php?act=list'));
    $smarty->assign('form_action', $form_action);

    assign_query_info();
    $smarty->display('banner_add.htm');
}

/*------------------------------------------------------ */
//-- 更新会员注册项
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'update')
{
    // var_dump(444);
    // var_dump($_POST);
    // var_dump($_FILES);exit;
    // admin_priv('banner');
    $img = '';
    // 生成图片
    if ($_FILES['img']['error'] === 0)
    {
        // $_FILES['img']['error'] ===0 说明有文件上传
        if(!get_file_suffix($_FILES['img']['name'], $allow_suffix))
        {
            sys_msg('您上传的图片格式不正确！');
        }
        $name = date('Ymd');
        for ($i = 0; $i < 6; $i++)
        {
            $name .= chr(mt_rand(97, 122));
        }
        $bbb = explode('.', $_FILES['img']['name']);
        $name .= '.' . end($bbb);
        $target = ROOT_PATH . DATA_DIR . '/banner/' . $name;
        if (move_upload_file($_FILES['img']['tmp_name'], $target))
        {
            $img = DATA_DIR . '/banner/' . $name;
        }
    }
    // var_dump($img);exit;
    $time = time();
    $id = $_POST['id'];  
    if($img){
        $sql = "UPDATE " .$ecs->table('banner') .
                " set `img` = '$img',`u_time` = $time ".
            " where `id` = $id";
    }else{
         $sql = "UPDATE " .$ecs->table('banner') .
                " set `u_time` = $time ".
            " where `id` = $id";
    }
    
    $db->query($sql);

    /* 管理员日志 */
    admin_log($img, 'add', 'banner');
    clear_cache_files();

    $lnk[] = array('text' => 'Banner设置',    'href'=>'banner.php?act=list');
    $lnk[] = array('text' => '继续添加banner', 'href' => 'banner.php?act=add');
    sys_msg('编辑Banner成功', 0, $lnk);
}

/*------------------------------------------------------ */
//-- 删除会员注册项
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('banner');

    $field_id = intval($_GET['id']);
    // $field_name = $exc->get_name($field_id);

    if ($exc->drop($field_id))
    {
        /* 删除会员扩展信息表的相应信息 */
        $sql = "DELETE FROM " . $GLOBALS['ecs']->table('banner') . " WHERE id = '" . $field_id . "'";
        @$GLOBALS['db']->query($sql);

        // admin_log(addslashes($field_name), 'remove', 'banner');
        clear_cache_files();
    }

    $url = 'banner.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;

}

/*
 *  编辑会员注册项名称
 */
elseif ($_REQUEST['act'] == 'edit_name')
{
    $id = intval($_REQUEST['id']);
    $val = empty($_REQUEST['val']) ? '' : json_str_iconv(trim($_REQUEST['val']));
    check_authz_json('banner');
    if ($exc->is_only('reg_field_name', $val, $id))
    {
        if ($exc->edit("reg_field_name = '$val'", $id))
        {
            /* 管理员日志 */
            admin_log($val, 'edit', 'banner');
            clear_cache_files();
            make_json_result(stripcslashes($val));
        }
        else
        {
            make_json_error($db->error());
        }
    }
    else
    {
        make_json_error(sprintf($_LANG['field_name_exist'], htmlspecialchars($val)));
    }
}

/*
 *  编辑会员注册项排序权值
 */
elseif ($_REQUEST['act'] == 'edit_order')
{
    $id = intval($_REQUEST['id']);
    $val = isset($_REQUEST['val']) ? json_str_iconv(trim($_REQUEST['val'])) : '' ;
    check_authz_json('banner');
    if (is_numeric($val))
    {
        if ($exc->edit("dis_order = '$val'", $id))
        {
            /* 管理员日志 */
            admin_log($val, 'edit', 'banner');
            clear_cache_files();
            make_json_result(stripcslashes($val));
        }
        else
        {
            make_json_error($db->error());
        }
    }
    else
    {
        make_json_error($_LANG['order_not_num']);
    }
}

/*------------------------------------------------------ */
//-- 修改会员注册项显示状态
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_dis')
{
    check_authz_json('banner');

    $id     = intval($_POST['id']);
    $is_dis = intval($_POST['val']);

    if ($exc->edit("display = '$is_dis'", $id))
    {
        clear_cache_files();
        make_json_result($is_dis);
    }
}

/*------------------------------------------------------ */
//-- 修改会员注册项必填状态
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_need')
{
    check_authz_json('banner');

    $id     = intval($_POST['id']);
    $is_need = intval($_POST['val']);

    if ($exc->edit("is_need = '$is_need'", $id))
    {
        clear_cache_files();
        make_json_result($is_need);
    }
}
?>